'use strict';
const { create_by_code, new_by_dockerImg } = require('../Middleware/mid_interface');
const Controller = require('egg').Controller;
var axios = require('axios');
const { url } = require('../../config/config');

class FuncNewController extends Controller {
    async get_docker() {
        const { ctx } = this;
        var config = {
            method: 'get',
            url: `http://${url}:5000/v2/_catalog`,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
        };
        var response = await axios(config);
        ctx.body = JSON.stringify(response.data);
    }
    async new_code() {
        const { ctx } = this;
        const { name, lang } = ctx.request.body;
        var ret_cmd = await create_by_code(name, lang);
        ctx.body = ret_cmd;
    }
    async new_docker() {
        const { ctx } = this;
        /**
         * docker镜像，函数名称
         * 从本地127.0.0.1:5000拉取
         */
        const { image, name } = ctx.request.body;
        var msg = await new_by_dockerImg(image, name);
        ctx.body = msg == "\"\"" ? "✅通过Docker镜像部署成功！" : msg;
    }
}

module.exports = FuncNewController;
/**
 * /new_code 
 * POST方法，用于手动部署函数
 * ·入参 
 *      token
 *      name 函数名称：nodeinfo
 *      files 上传的文件：[{"filename":"文件名","filestream":"文件的文本流",}]
 * ·回参 
 *      code 1-成功 0-失败
 *      msg 返回的提示消息："部署成功！"
 * 
 * /new_docker 
 * POST方法，用于通过Dokcer镜像部署函数
 * ·入参 
 *      token
 *      image 镜像名称：127.0.0.1:5000/hellofaas
 * ·回参 
 *      code 1-成功 0-失败
 *      msg 返回的提示消息："部署成功！"
 */